package net.sf.nwn.jme3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.jme3.asset.AssetManager;

public class ControllableModelPrototype {

	private ModelPrototype baseModel;
	private HashMap<String, ModelAnimation> animations = new HashMap<String,ModelAnimation>();
	private ArrayList<String> defAnims;
	private AssetManager manager;
	private ShadingType metallic;

	public ControllableModelPrototype(AssetManager manager, String modelName, ShadingType metallic ) {
		this.baseModel = (ModelPrototype) manager.loadAsset(modelName);
		this.manager = manager;
		this.metallic = metallic;
		if ( baseModel == null ) {
			throw new IllegalArgumentException("Cannot find model " + modelName);
		}
		
		ModelPrototype supermodel = baseModel;
		while ( supermodel != null ) {
			Map<String, ModelAnimation> superAnimations = supermodel.getAllAnimations();
			for ( String animName : superAnimations.keySet() ) {
				if ( !animations.containsKey(animName)) {
					animations.put(animName,superAnimations.get(animName));
				}
			}
			supermodel = supermodel.getSupermodel(manager);
		}
		 
		baseModel.fixupEmitters();
		
		this.defAnims = new ArrayList<String>();

		for ( String name : animations.keySet() ) {
			if (name.indexOf("pause") >= 0 ||name.indexOf("hturn") >= 0 ) {
                defAnims.add(name);
            }
		}

	}
	
	public ControllableModel createModel() {
		return new ControllableModel(this,manager, metallic);
	}
	
	public ModelPrototype getBaseModel() {
		return baseModel;
	}
	
	public Map<String, ModelAnimation> getAnimations() {
		return animations;
	}
	
	public ArrayList<String> getDefAnims() {
		return defAnims;
	}

	public String getName() {
		return baseModel.getName();
	}
	
}
